import 'dart:convert';

main(){
  User user=User('张三',18,'男');

  print(user.age);
  //后端回来的数据
  String jsonStr='{"name":"张三","age":18,"gender":"男"}';
  //将json字符串转换为Map
  Map jsonMap=json.decode(jsonStr);
  print(jsonMap is Map);
  print('用户名是： ${jsonMap['name']}');

  //把map转换为对象
  user=User.fromJson(jsonMap);
  print(user.name);
  print(user.age);
  print(user.gender);

}

class User{
  late String name;
  late int age;
  late String gender;

  User(this.name,this.age,this.gender);

  // 命名构造函数(次)，传入 Map 初始化数据
  User.fromJson(Map jsonMap) {
    name = jsonMap['name'];
    age = jsonMap['age'];
    gender = jsonMap['gender'];
  }
}
